<%-- 
    Document   : index
    Created on : 11/11/2008, 10:25:07
    Author     : tiago
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*,java.util.*,br.com.radaction.DAO.*,br.com.radaction.DTO.*,java.text.SimpleDateFormat,java.math.BigDecimal,java.text.ParseException" %>
<%@ page import="java.sql.*" %>
<%@ include file="validasessao.jsp" %>
<%@ include file="conexao.jsp" %>
<%
        String acao = request.getParameter("acao");
        Curso curso = null;
        if (acao != null) {

            if ("salvar".equals(acao)) {
                String nome = request.getParameter("txtNome");
                Curso cur = new Curso();
                cur.setNome(nome);
                int codigo = 0;
                try {
                    codigo = Integer.parseInt(request.getParameter("txtCodigo"));
                } catch (NumberFormatException ex) {
                } catch (NullPointerException ex) {
                }
                CursoDAO curDAO = new CursoDAO(conn);
                try {
                    if (codigo == 0) {
                        curDAO.create(cur);
                    } else {
                        cur.setCod(codigo);
                        curDAO.update(cur);
                    }
                } catch (SQLException ex) {
                    request.setAttribute("mensagem", String.format("Não foi possível inserir Curso.[%s]", ex.getMessage()));
                    RequestDispatcher dispatcher = request.getRequestDispatcher("formerro.jsp");
                    dispatcher.forward(request, response);
                    if (conn != null) {
                        conn.close();
                    }
                    return;
                }
            } else if ("excluir".equals(acao)) {
                int codigo = 0;
                try {
                    codigo = Integer.parseInt(request.getParameter("txtCodigo"));
                } catch (NumberFormatException ex) {
                } catch (NullPointerException ex) {
                }
                CursoDAO curDAO = new CursoDAO(conn);
                try {
                    if (codigo > 0) {
                        Curso cur = new Curso(codigo, request.getParameter("txtNome"));
                        curDAO.delete(cur);
                    }
                } catch (SQLException ex) {
                    request.setAttribute("mensagem", String.format("Não foi possível excluir curso.[%s]", ex.getMessage()));
                    RequestDispatcher dispatcher = request.getRequestDispatcher("formerro.jsp");
                    dispatcher.forward(request, response);
                    if (conn != null) {
                        conn.close();
                    }
                    return;
                }
            } else if ("carregar".equals(acao)) {
                int codigo = 0;
                try {
                    codigo = Integer.parseInt(request.getParameter("txtCodigo"));
                } catch (NumberFormatException ex) {
                } catch (NullPointerException ex) {
                }
                CursoDAO curDAO = new CursoDAO(conn);
                try {
                    if (codigo > 0) {
                        Curso cur = new Curso(codigo);
                        curso = curDAO.retrieve(cur);
                    }
                } catch (SQLException ex) {
                    request.setAttribute("mensagem", String.format("Não foi possível ler curso.[%s]", ex.getMessage()));
                    RequestDispatcher dispatcher = request.getRequestDispatcher("formerro.jsp");
                    dispatcher.forward(request, response);
                    if (conn != null) {
                        conn.close();
                    }
                    return;
                }
            }
        }
%>
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>RADAcademics Interface Web</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" media="screen"/>
    </head>
    <script type="text/javascript">
        function limpar(){
            document.getElementById("txtCodigo").value="";
            document.getElementById("txtNome").value="";
        }

        function salvar(){
            document.getElementById("acao").value="salvar";
            document.formCurso.submit();
        }

        function excluir(){
            document.getElementById("acao").value="excluir";
            document.formCurso.submit();
        }

        function atualizar(){
            document.getElementById("acao").value="atualizar";
            document.formCurso.submit();
        }

        function clickTabela(cod){
            document.getElementById("txtCodigo").value=cod;
            document.getElementById("acao").value="carregar";
            document.formCurso.submit();
        }
    </script>
    <body>

        <div class="wrapper">

            <div class="container">

                <div class="main">


                    <div class="content">
                        <form id="formCurso" name="formCurso" action="cursos.jsp" method="POST">

                            <div class="title"><img src="images/logorad2.png" width="150"></div>

                            <h2>Cursos</h2>
                            <div class="descr"><%= new java.util.Date()%></div>

                            <label class="labels" for="txtCodigo">Codigo:</label><input style="color:red;background-color:silver" size="1" size="3" readonly="readonly" class="inputs" id="txtCodigo" type="text" name="txtCodigo" value="<%=("carregar".equals(acao)) ? curso.getCod() : ""%>"/>
                            <br/>
                            <br/>
                            <label class="labels" for="txtNome">Curso:&nbsp;&nbsp;</label><input  size="50" class="inputs" id="txtNome" type="text" name="txtNome" value="<%=("carregar".equals(acao)) ? curso.getNome() : ""%>"/>
                            <br/>
                            <br/>
                            <input class="buttons" type="button" onclick="limpar()" value="Limpar"/>
                            <input class="buttons" type="button" onclick="salvar()" value="Salvar"/>
                            <input class="buttons" type="button" onclick="excluir()" value="Excluir"/>
                            <input class="buttons" type="submit" value="Atualizar"/>
                            <input type="hidden" id="acao" name="acao" value="nula"/>
                            <br/>
                            <br/>

                            <div class="tabela">
                                <table style="border:dotted;width:100%" border="0">
                                    <thead class="cabecalhoTabela">
                                        <tr style="background-color:gray"><td>Código</td><td>Nome</td></tr>
                                    </thead>
                                    <tbody>
                                        <%
        CursoDAO curDAO2 = new CursoDAO(conn);
        List<Curso> cursos = curDAO2.listaTodos();
        for (Curso cur : cursos) {
            out.print(String.format("<tr onClick=\"clickTabela(%d)\" class=\"linhaTabela\">", cur.getCod()));
            out.print(String.format("<td class=\"linhaTabela\">%s</td><td>%s</td>", cur.getCod(), cur.getNome()));
            out.print("</tr>");
        }
                                        %>
                                    </tbody>
                                </table>
                            </div>
                        </form>
                    </div>

                    <div class="sidenav">

                        <h2>Login</h2>
                        <ul>
                            <form action="sair.jsp">
                                <li><input type="submit" value="Sair" name="Sair" /></li>
                            </form>

                        </ul>

                        <h2>Menu</h2>
                        <ul>
                            <li><a href="cursos.jsp">Cursos</a></li>
                            <li><a href="disciplinas.jsp">Disciplinas</a></li>
                            <li><a href="avaliacoes.jsp">Avaliações</a></li>
                        </ul>

                    </div>

                    <div class="clearer"></div>

                </div>

                <div class="footer">

                    <div class="left">&copy; 2008 <a href="http://www.radaction.com.br">radaction.com</a>. Valid <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> &amp; <a href="http://validator.w3.org/check?uri=referer">XHTML</a></div>

                    <div class="right">Design by <a href="http://arcsin.se/">R4D4C710N</a> <a href="http://templates.arcsin.se/">Web Templates</a></div>

                    <div class="clearer"><span></span></div>

                </div>

            </div>

        </div>

    </body>

</html>
<%
        if (conn != null) {
            conn.close();
        }
%> 